// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

// standard headers
/*
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
#include <cstdarg>
#include <stdexcept>
#include <utility>
#include <tuple>

#include <cmdioutil.h>*/
#include "param_interpret.h"

int main(int argc, char** argv)
{
try
{   
	cmd::arguments arg(argc,argv);
	cmd::switcharg<false> help(arg,"-h","--help", "print help message");
        cmd::switcharg<false> stdio(arg,"-std","--std", "standard input and output");
        cmd::switcharg<false> print_out(arg,"-p","--print", "print out result (and no overwrite)");

        xyz c;
        if(*stdio) load(std::cin,c);

        cmd::vararg<std::string> output_fn (arg,"-o","--output","ouput file name","output");
//	cmd::vararg<util::list_expression<int> > selected_range (arg,"-s","--select","select atoms, by default all selected","");
//        cmd::multivararg<std::string> selected_range (arg,"-s","--select","(repeatable opt) select atoms, by default all selected");
        
//        cmd::switcharg<false> invert_selection(arg,"-i","--invert-selection", "invert selection range");
        //////////////////////////////////////////////////////////////////////////


        cmd::vararg<std::string> uc_a1 (arg,"-a1","--a1","@1 expr sets a1 lattice vector","");
        cmd::vararg<std::string> uc_a2 (arg,"-a2","--a2","@1 expr sets a2 lattice vector","");
        cmd::vararg<std::string> uc_a3 (arg,"-a3","--a3","@1 expr sets a3 lattice vector","");

        cmd::vararg<std::string> uc_bb (arg,"-bb","--bbox","create bounding box as unit cell @1 sep. between objects","");
        cmd::vararg<std::string> uc_bbm (arg,"-bbm","--bboxm","create bounding box as unit cell @1 sep between objects and put to the middle","");
        cmd::switcharg<false> uc_m(arg,"-m","--middle", "place the objects in the middle of the unit cell");

        
        ///////////////////////////////////////////////////////////////////////////
        std::string fn;
        if(*help || !*stdio)
        {
	        cmd::posarg<std::string> io_fn(arg,"input file name", "");
                if(!*help) { fn = *io_fn; load(fn,c); }
        }
	
        
	if(*help)
	{
		arg.print_help(std::cout,"xyzuc");
                std::cout << std::endl;
                std::cout << interpret_expression_help();                
                std::cout << std::endl;
                std::cout << "examples:" << std::endl;
                std::cout << "   xyzuc m.xyz -a1 1/0/0 -a2 0/1/0 -a3 0/0/1  #set a1,a2 and a3 vectors"<< std::endl;
                std::cout << "   xyzuc m.xyz -a1 1%-1 -a2 0/1/0 -a3 0/0/1   #set lattice vectors by using coord. expr."<< std::endl;
                std::cout << "   xyzuc m.xyz -a1 x%14%12+x%14%2             #set a1 vector by using coord. expr."<< std::endl;
                std::cout << std::endl;
                std::cout << "By default m.xyz will be overwritten!"<< std::endl;
                std::cout << std::endl;
                std::cout << "Version 0.9b"<< std::endl;
                std::cout << "Copyright (c) 2010 David Zsolt Manrique (david.zsolt.manrique@gmail.com)"<< std::endl;
    		return EXIT_SUCCESS;
	}
        ///////////////////////////////////////////////////////////////////////////
//        if(~selected_range) for(int i=0;i<(*selected_range).size();i++) select_by_expression(c,(*selected_range)[i]); else neg(c);
//        if(*invert_selection) neg(c);
        ////////////////////////////////////////////////////

        Eigen::Vector3d a1,a2,a3,na1,na2,na3;
        a1 = c.lattice[0];
        a2 = c.lattice[1];
        a3 = c.lattice[2];
//        na1 = a1.normalized();
//        na2 = a2.normalized();
//        na3 = a3.normalized();
        
        neg(c);
        if(~uc_a1) c.lattice[0] = interpret_coord(c,*uc_a1);
        else if(~uc_a2) c.lattice[1] = interpret_coord(c,*uc_a2);
        else if(~uc_a3) c.lattice[2] = interpret_coord(c,*uc_a3);
        else if(~uc_bb) {
                Eigen::Vector3d bboxmin,bboxmax;   
                bbox(c,bboxmin,bboxmax);
                Eigen::Vector3d bbox;
                bbox = bboxmax-bboxmin + interpret_coord(c,*uc_bb);
                c.lattice[0] << bbox.x(),0,0;
                c.lattice[1] << 0,bbox.y(),0;
                c.lattice[2] << 0,0,bbox.z();
        }
        else if(~uc_bbm) {
                Eigen::Vector3d bboxmin,bboxmax;   
                bbox(c,bboxmin,bboxmax);
                Eigen::Vector3d sep;
                sep = interpret_coord(c,*uc_bb);
                Eigen::Vector3d bbox;
                bbox = bboxmax-bboxmin + sep;
                Eigen::Vector3d shift;
                shift = -bboxmin + 0.5*sep;
                c += shift;
                        
                c.lattice[0] << bbox.x(),0,0;
                c.lattice[1] << 0,bbox.y(),0;
                c.lattice[2] << 0,0,bbox.z();
        }
        else if(~uc_m) {
                Eigen::Vector3d bboxmin,bboxmax;   
                bbox(c,bboxmin,bboxmax);
                Eigen::Vector3d cellmiddle = (c.lattice[0]+ c.lattice[1]+ c.lattice[2])*0.5;
                Eigen::Vector3d boxmiddle = (bboxmax-bboxmin)*0.5;
                Eigen::Vector3d shift;
                shift = cellmiddle-boxmiddle;
                c -= bboxmin;
                c += shift;
        }
        


        ////////////////////////////////////////////////////
        if(*print_out) print(std::cout,c);
        else if(*stdio) save(std::cout,c);
        else if(~output_fn) save(*output_fn,c);
        else save(fn,c);

} catch(const char* message)
{
	std::cerr << message << std::endl;
	std::cerr << "(exit)" << std::endl;
	return EXIT_FAILURE;
} 
catch(const std::exception & e)
{
        std::cerr << e.what() << std::endl;
        std::cerr << "(exit)" << std::endl;
        return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}


